package com.uinnova.springboot.mybatis.service.Impl;

import com.uinnova.springboot.mapper.SysUserMapper;
import com.uinnova.springboot.model.SysUser;
import com.uinnova.springboot.mybatis.service.SysUserService;
import org.checkerframework.checker.units.qual.A;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheConfig;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.CachePut;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;
/*
*@author 王刚
*@date 2019-01-07 15:08
*实现sysuser的增删该查
* @cacheable注解 方法在执行先到缓存中查询数据，缓存没有在执行方法，然后将返回结果存入缓存
* @cacheput注解方法一定执行，执行完将返回结果存入缓存 update数据时，key的值要和@cacheable注解的值一样
* @CacheEvit 注解，根据key删除缓存中的对应注解
*/

@Service
@CacheConfig(cacheNames = "cache_sysuser")
public class SysUserServiceImpl implements  SysUserService{

    @Autowired
    private SysUserMapper sysUserMapper;


//    @CachePut(key = "#sysUser.sysId")
//    增加用户
    public SysUser addSysUser(SysUser sysUser) {
        System.out.println("报告老大，sysUser插入完毕");
        sysUserMapper.insert(sysUser);
        return sysUser;
//        sysUserMapper.insertSelective(sysUser);

    }

    @CacheEvict(key = "#id")
    public int  deleteSysUser(Integer id) {
        System.out.println("报告老大，sysUser删除完毕");
        return sysUserMapper.deleteByPrimaryKey(id);
    }

    @CachePut(key = "#sysUser.sysId")
    public SysUser  updateSysUser(SysUser sysUser) {
        System.out.println("报告老大，sysUser更新完毕");
        sysUserMapper.updateByPrimaryKey(sysUser);
        return  sysUser;
//        sysUserMapper.updateByPrimaryKeySelective(sysUser);

    }

    @Cacheable(key = "#id")
    public SysUser queryById(Integer id) {
        SysUser user =sysUserMapper.selectByPrimaryKey(id);
        System.out.println("报告老大，sysuser给您查询好喽");
        return user;
    }
}
